package com.capgemini.poste.metier;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.capgemini.poste.dao.Affectation;
import com.capgemini.poste.dao.Event;

public class Owner extends com.capgemini.poste.dao.Affectation {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 
	 */

	static HashSet<Affectation> affectations = new HashSet<Affectation>() ;
	
	public Owner ( EntityManager em ) {
		
		// Create the query and populate the HashSet with workstations retrieved from DTB
		Query getAllAffectation = em.createQuery("select a from Affectation a") ;
		@SuppressWarnings("unchecked")
		List<Affectation> results = getAllAffectation.getResultList() ;
		for ( Affectation result : results ) {
			affectations.add(result) ;
		}

	}

	private ArrayList<Event> retrieve (XSSFSheet excelSheet, Date date ) {
		// TODO Auto-generated method stub
		XSSFRow row;
		boolean bFirst = true ;
		ArrayList<Event>events = new ArrayList<Event> () ;

		for (Iterator<Row> rowIterator = excelSheet.rowIterator(); rowIterator.hasNext();) {
			row = (XSSFRow) rowIterator.next();
            // Skip the header row
			if ( bFirst == true) {
               row = (XSSFRow) rowIterator.next();
               bFirst = false ;
            }
			
			Affectation affectation = new Affectation ();
			// Colonne 0 = Code du poste
			if ( row.getCell(0).getCellType() == HSSFCell.CELL_TYPE_NUMERIC ){
				affectation.setPosteCd(String.valueOf(row.getCell(0).getNumericCellValue()));
			}
			else {	
				affectation.setPosteCd(row.getCell(0).getStringCellValue().toUpperCase());
			}
			
			// Colonne 1 = Login
			affectation.setUserCd(row.getCell(1).getStringCellValue().toUpperCase());
			affectation.setDt(date) ;
			
			// Use a HashSet avoid multiple records
			if ( affectations.add(affectation) == true ) {
				System.out.println ( "Link between " + affectation.getPosteCd() + " and " + affectation.getUserCd() + " has been added !!");
				Event event = new Event () ;
				event.setType(POSTE.LINK) ;
				event.setCd(affectation.getPosteCd()+" and " + affectation.getUserCd() ) ;
				events.add(event) ;
			}

		}
		return events ;
	}

	public ArrayList<Event> retrieve (XSSFWorkbook excelWorkbook, Date date ) {
		// TODO Auto-generated method stub
		ArrayList<Event> events = new ArrayList<Event> () ;

		events = this.retrieve(excelWorkbook.getSheetAt(0), date) ;
		events.addAll(this.retrieve ( excelWorkbook.getSheetAt(1), date) ) ;
		events.addAll(this.retrieve ( excelWorkbook.getSheetAt(2), date) ) ;
		
		return events ;
	}
	
	public int getSize ( ) {
		return affectations.size() ;
	}
	
	public void save ( EntityManager em ) {
		for ( Iterator<Affectation> p = affectations.iterator(); p.hasNext() ;) {
			em.persist(p.next()) ;
		}
	}
		

}
